package com.edu.controller;


import com.R;
import com.edu.entity.Chapter;
import com.edu.entity.chapter.ChapterVo;
import com.edu.service.impl.ChapterServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-17
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class ChapterController {

	@Autowired
	private ChapterServiceImpl chapterService;

	//课程大纲列表，根据课程id进行查询
	@GetMapping("/getChapterVider/{courseId}")
	public R getChapterVider(@PathVariable String courseId){
		List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);

		return R.ok().data("allChapterVider",list);
	}

	//添加章节
	@PostMapping("/addChapter")
	public R addChapter(@RequestBody Chapter chapter){
		chapterService.save(chapter);
		return R.ok();
	}

	//根据id查询章节
	@GetMapping("/getChapterInfo/{chapterId}")
	public R getChapterInfo(@PathVariable String chapterId){

		Chapter chapter = chapterService.getById(chapterId);
		return R.ok().data("chapter",chapter);
	}

	//修改章节
	@PostMapping("/updateChapter")
	public R updateChapter(@RequestBody Chapter chapter){
		chapterService.updateById(chapter);
		return R.ok();
	}

	//
	@DeleteMapping("/{chapterId}")
	public R deleteChapter(@PathVariable String chapterId){
		boolean flag = chapterService.deleteChapter(chapterId);
		if (flag){
			return R.ok();
		}else {
			return R.error();
		}
	}
}

